package Sisth;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Root extends Person implements operate {
    //继承了人的类，需要重写它的抽象方法operate，实现了operate接口，需要重写接口内的方法

    public Root(String name, String sex, int age) {
        //调用人基类的构造函数
        super(name, sex, age);
    }

    Scanner sc = new Scanner(System.in);

    /**
     * 管理员的操作
     */

    public void operate() {
        System.out.print("请输入确认操作:");
        int i = sc.nextInt();
        switch (i) {
            case 1:
                //整理
                sort(books);
                break;
            case 2:
                //查
                select(books);
                break;
            case 3:
                //加
                add(books);
                break;
            case 4:
                //删
                delete(books);
                break;
            case 5:
                //列表
                list(books);
                break;
            default:
                System.out.println("输入有误！");
                break;

        }
    }

    /**
     * 	compareable整理，重写compare to，如果数组中有空的，则会出现空指针异常，把不空的拷到另一个新的数组，然后实现
     * @param books
     */
    public void sort(Book[] books) {
        System.out.println("图书馆中的书按价格整理如下:" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态");
        Arrays.sort(books, new Comparator<Book>() {

            public int compare(Book o1, Book o2) {
                return (int) (o1.getPrice() - o2.getPrice());
            }
        });
        //遍历数组打印书的列表
        for (int i = 0; i < books.length; i++) {
            if (books[i].price != 0) {
                System.out.println(
                        "【" + books[i].getName() + "】—— 【" + books[i].getAuthor() + "】——【" + books[i].getPrice()
                                + "】——【" + books[i].getCategory() + "】——" + books[i].getState());
            }
        }
    }


    public void select(Book[] books) {
        System.out.println("请输入书名进行查询：");
        String name = sc.next();
        if (name != null) {
            boolean flag = false;
            int bookNum = 0;
            for (int i = 0; i < books.length; i++) {
                if (books[i].getName() != null && name.equals(books[i].getName())) {
                    flag = true;
                    bookNum = i;
                    break;
                }
            }
            if (!flag) {
                System.out.println("未查询到此书籍！");
            } else {
                System.out.println("书籍信息为：" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态");
                System.out.println(
                        "【" + books[bookNum].getName() + "】—— 【" + books[bookNum].getAuthor() + "】——【" + books[bookNum]
                                .getPrice() + "】——【" + books[bookNum].getCategory() + "】——" + books[bookNum]
                                .getState());
            }
        }
    }

    public void delete(Book[] books) {
        System.out.println("请输入要删除的书名:");
        String str = sc.next();
        if (str != null) {
            for (int i = 0; i < books.length; i++) {
                if (books[i].getName().equals(str)) {
                    books[i] = null;
                    System.out.println("删除成功！");
                    break;
                }
            }
        }

    }


    public void add(Book[] books) {
        System.out.println("请输入书名：");
        String name = sc.next();
        System.out.println("请输入作者：");
        String author = sc.next();
        System.out.println("请输入价格：");
        double price = sc.nextDouble();
        System.out.println("请输入分类：");
        String category = sc.next();

        boolean flag = false;
        for (int i = 0; i < books.length; i++) {
            if (books[i].getName() == null) {
                flag = true;
                books[i].setName(name);
                books[i].setAuthor(author);
                books[i].setPrice(price);
                books[i].setCategory(category);
                books[i].setState(true);
                System.out.println("添加成功");
                break;
            }
        }
        if (!flag) {
            System.out.println("【警告】数组预留buffer最多只能添加3本新书,没有位置放置新书了，如需添加多余3本，需要在‘Person’类中添加buffer！" + "\n");
        }

    }


    public void list(Book[] books) {
        System.out.println("图书列表如下:" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态");  //打印Book数组
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                if (books[i].price != 0) {
                    System.out.println(
                            "【" + books[i].getName() + "】—— 【" + books[i].getAuthor() + "】——【" + books[i].getPrice()
                                    + "】——【" + books[i].getCategory() + "】——" + books[i].getState());

                }
            }
        }
    }
}
